package com.framework.modules.job.task.yhbtask;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.framework.entity.sys.SysPayment;
import com.framework.service.PaymentService;
import com.framework.service.yhbservice.YhbFlowTakeService;
import com.framework.service.yhbservice.YhbUserService;
import com.framework.utils.pay.yunzhanghujiesuan.YunJieSuanPay;
import com.framework.yhbEntity.transaction.YhbFlowTake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 用户宝提现审查
 */
@Component("yhbFlowTakeTask")
public class YhbFlowTakeTask {

    @Autowired
    private YhbFlowTakeService yhbFlowTakeService;

    @Autowired
    private PaymentService paymentService;

    @Autowired
    private YhbUserService yhbUserService;


    private Logger logger= LoggerFactory.getLogger(getClass());

    public void flowTakeTask() throws Exception {

        logger.info("-----------------修改用户宝提现信息开始---------------------");
        List<YhbFlowTake> list = yhbFlowTakeService.selectList(new EntityWrapper<YhbFlowTake>().eq("take_type", 4).eq("dept_id", 2)
            .eq("take_state","EXAMINE")
        );
        SysPayment payment = paymentService.queryPaymentByDeptId(2L);
        YhbFlowTake yhbFlowTake;
        Map<String, Object> result;
        for (int i = 0; i < list.size(); i++) {
            yhbFlowTake= list.get(i);
            result = YunJieSuanPay.queryOrder(yhbFlowTake.getYhbTakeNo(),payment);
            handleYunjiesuanResult(result,yhbFlowTake.getYhbTakeNo());
        }
        logger.info("-----------------修改用户宝提现信息成功---------------------");
    }


    private void handleYunjiesuanResult(Map<String, Object> result, String takeNo) {
        if(result==null){
            logger.info("----------云账号未查询到订单号为"+takeNo+"的信息--------------");
            return;
        }
        System.out.println(result);
        String code=result.get("code").toString();
        if(!"0000".equals(code)){
            logger.info("-------订单号为:"+takeNo+"错误信息为"+result.get("message")+"----------");
        }
        Map<String,Object> data= (Map<String, Object>) result.get("data");
        String status=data.get("status").toString();
        YhbFlowTake flowTask = yhbFlowTakeService.selectById(takeNo);
        String userId = flowTask.getUserId();
        if(flowTask==null){
            logger.info("----------未查询到订单号为"+takeNo+"的订单提现---------------");
        }
        if("1".equals(status)){
            flowTask.setTakeState("SUCCESS");
            flowTask.setExplain("提现成功");

        }else if("2".equals(status) || "9".equals(status)){
            flowTask.setTakeState("FAILURE");
            flowTask.setExplain("提现失败---"+data.get("status_detail_message").toString());
            //返还金额
            yhbUserService.addBalance(userId,flowTask.getTakeAmount());
        }
        yhbFlowTakeService.updateById(flowTask);


    }
}